package com.frank.http.encryp;

import java.io.ByteArrayOutputStream;
import java.util.Map.Entry;
import java.util.SortedMap;

import android.content.Context;

import com.frank.http.base.AsyncHttpClient;
import com.frank.http.expand.Logger;
import com.frank.http.expand.PreferenceAppService;

/**
 * 添加网络加密请求头
 * 
 * @author Frank
 * @version 1.0 Create by 2016.3.25
 */
public class HttpHeader {
	public static final String AUTHORIZATION = "Authorization";
	public static final String CONTENT_TYPE = "Content-Type";
	public static final String CONTENT_LENGTH = "Content-Length";
	public static final String CONTENT_TYPE_CONTENT = "application/x-www-form-urlencoded; charset=UTF-8";
	public static final String CONTENT_TYPE_CONTENT_FROM_DATA = "multipart/form-data";

	public static void addAuthorization(Context context,
			AsyncHttpClient client, HttpMethod post, String url,
			ByteArrayOutputStream content, boolean isFormData) throws Exception {
		if (client == null) {
			throw new EncrypException("addAuthorization():client不能为空");
		}
		if (content == null) {
			throw new EncrypException("addAuthorization():content不能为空");
		}
		if (url == null || url.equals("")) {
			throw new EncrypException("addAuthorization():url不能为空");
		}

		String auth_string = "";

		try {
			auth_string = EncrypUtils.getAuthStringApp(
					EncrypUtils.getUUId(),
					PreferenceAppService.getInstance().getItemAndroid("session_id"),
					PreferenceAppService.getInstance().getItemAndroid("session_card")
							.equals("0") ? EncrypUtils.getUid(context)
							: PreferenceAppService.getInstance().getItemAndroid(
									"session_card"), EncrypUtils.getUTC(),
					"1800");
		} catch (Exception e) {
			e.printStackTrace();
		}

		SortedMap<String, String> headParameters = null;
		try {
			headParameters = HttpUtil.getHeaderParameters(url, content,
					isFormData);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (headParameters == null) {
			throw new EncrypException("addAuthorization():headParameters不能为空");
		}

		String session_key = PreferenceAppService.getInstance()
				.getItemAndroid("session_key").equals("0") ? "session_key"
				: PreferenceAppService.getInstance().getItemAndroid("session_key");
		Logger.i("session_key", session_key);
		auth_string = EncrypUtils
				.getAuthString(
						auth_string,
						EncrypUtils.getSignedHeaders(headParameters),
						EncrypUtils.getSigningKey(
								EncrypUtils
										.getCanonicalRequest(
												post.toString(),
												HttpUtil.getCanonicalURI(url),
												EncrypUtils
														.getCanonicalQueryString(url),
												EncrypUtils
														.getCanonicalHeaders(headParameters)),
								EncrypUtils.getSigningKey(
										auth_string,
										PreferenceAppService.getInstance()
												.getItemAndroid("session_key")
												.equals("0") ? "session_key"
												: PreferenceAppService
														.getInstance().getItemAndroid(
																"session_key"))));

		headParameters.put(AUTHORIZATION, auth_string);

		addPoshHeader(client, headParameters);
	}
	
	
	public static void addAuthorization(Context context,
			AsyncHttpClient client, HttpMethod post, String url,
			ByteArrayOutputStream content, boolean isFormData,int type) throws Exception {
		if (client == null) {
			throw new EncrypException("addAuthorization():client不能为空");
		}
		if (content == null) {
			throw new EncrypException("addAuthorization():content不能为空");
		}
		if (url == null || url.equals("")) {
			throw new EncrypException("addAuthorization():url不能为空");
		}
		
		String auth_string = "";
		
		try {
			auth_string = EncrypUtils.getAuthStringApp(
					EncrypUtils.getUUId(),
					PreferenceAppService.getInstance().getItemAndroid("session_id"),
					PreferenceAppService.getInstance().getItemAndroid("session_card")
					.equals("0") ? EncrypUtils.getUid(context)
							: PreferenceAppService.getInstance().getItemAndroid(
									"session_card"), EncrypUtils.getUTC(),
					"1800");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		SortedMap<String, String> headParameters = null;
		try {
			headParameters = HttpUtil.getHeaderParameters(url, content,
					isFormData,type);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (headParameters == null) {
			throw new EncrypException("addAuthorization():headParameters不能为空");
		}
		
		String session_key = PreferenceAppService.getInstance()
				.getItemAndroid("session_key").equals("0") ? "session_key"
						: PreferenceAppService.getInstance().getItemAndroid("session_key");
		Logger.i("session_key", session_key);
		auth_string = EncrypUtils
				.getAuthString(
						auth_string,
						EncrypUtils.getSignedHeaders(headParameters),
						EncrypUtils.getSigningKey(
								EncrypUtils
								.getCanonicalRequest(
										post.toString(),
										HttpUtil.getCanonicalURI(url),
										EncrypUtils
										.getCanonicalQueryString(url),
										EncrypUtils
										.getCanonicalHeaders(headParameters)),
										EncrypUtils.getSigningKey(
												auth_string,
												PreferenceAppService.getInstance()
												.getItemAndroid("session_key")
												.equals("0") ? "session_key"
														: PreferenceAppService
														.getInstance().getItemAndroid(
																"session_key"))));
		
		headParameters.put(AUTHORIZATION, auth_string);
		
		addPoshHeader(client, headParameters);
	}

	/***
	 * 添加post请求的头
	 */
	public static void addPoshHeader(AsyncHttpClient client,
			SortedMap<String, String> parameters) {
		for (Entry<String, String> entry : parameters.entrySet()) {
			String key = (String) entry.getKey();
			if (key == null || key.equals("")) {
				continue;
			}
			String value = (String) entry.getValue();
			if (value == null) {
				value = "";
			}
			client.addHeader(key, value);
		}
	}
}
